package com.okex.demo.config;

import com.okex.open.api.config.APIConfiguration;
import com.okex.open.api.service.account.AccountAPIService;
import com.okex.open.api.service.account.impl.AccountAPIServiceImpl;
import com.okex.open.api.service.marketData.MarketDataAPIService;
import com.okex.open.api.service.marketData.impl.MarketDataAPIServiceImpl;
import com.okex.open.api.service.publicData.PublicDataAPIService;
import com.okex.open.api.service.publicData.impl.PublicDataAPIServiceImpl;
import com.okex.open.api.service.trade.TradeAPIService;
import com.okex.open.api.service.trade.impl.TradeAPIServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 欧易API配置
 */
@Configuration
public class OkexApiConfig {

    @Value("${okex.api.endpoint:https://www.okx.com}")
    private String endpoint;

    @Value("${okex.api.apiKey:}")
    private String apiKey;

    @Value("${okex.api.secretKey:}")
    private String secretKey;

    @Value("${okex.api.passphrase:}")
    private String passphrase;

    /**
     * 是否使用模拟交易
     * 0: 实盘交易环境
     * 1: 模拟交易环境
     */
    @Value("${okex.api.simulatedTrading:0}")
    private String simulatedTrading;

    @Bean
    public APIConfiguration apiConfiguration() {
        APIConfiguration config = new APIConfiguration();
        config.setDomain(endpoint);
        config.setApiKey(apiKey);
        config.setSecretKey(secretKey);
        config.setPassphrase(passphrase);
        config.setxSimulatedTrading(simulatedTrading);
        config.setPrint(true);
        return config;
    }

    /**
     * 账户API服务，用于查询持仓信息
     */
    @Bean
    public AccountAPIService accountAPIService(APIConfiguration config) {
        return new AccountAPIServiceImpl(config);
    }

    /**
     * 市场数据API服务，用于获取行情信息
     */
    @Bean
    public MarketDataAPIService marketDataAPIService(APIConfiguration config) {
        return new MarketDataAPIServiceImpl(config);
    }

    /**
     * 公共数据API服务，用于获取系统时间等信息
     */
    @Bean
    public PublicDataAPIService publicDataAPIService(APIConfiguration config) {
        return new PublicDataAPIServiceImpl(config);
    }
    
    /**
     * 交易API服务，用于下单、平仓等交易操作
     */
    @Bean
    public TradeAPIService tradeAPIService(APIConfiguration config) {
        return new TradeAPIServiceImpl(config);
    }
}
